package org.glut.saltedfish.servlet;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.glut.saltedfish.domain.ChatRecords;
import org.glut.saltedfish.service.ChatRecordsServiceImpl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/chat")
public class ChatServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=utf-8");

        try(
            BufferedReader br = req.getReader();
            PrintWriter out = resp.getWriter();
            ){
            StringBuilder sb = new StringBuilder();

            String line;

            while ((line = br.readLine()) != null) {
                sb.append(line);
            }

            Gson gson = new GsonBuilder()
                    .setDateFormat("yyyy-MM-dd HH:mm:ss")  // 指定日期格式
                    .create();

            //反序列化接受的 json
            ChatRecords record = gson.fromJson(sb.toString(), ChatRecords.class);

            record.setStatus(0);

            System.out.println(record);

            Integer i = new ChatRecordsServiceImpl().insertChatRecords(record);



            // 构建返回结果
            Map<String, Object> result = new HashMap<>();
            if (i != null && i > 0) {
                result.put("success", true);
                result.put("message", "消息发送成功");
                result.put("data", record);
                resp.setStatus(HttpServletResponse.SC_OK);
            } else {
                result.put("success", false);
                result.put("message", "消息发送失败");
                resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            }

            // 返回JSON响应
            out.print(new Gson().toJson(result));
            out.flush();

        } catch (JsonSyntaxException e) {
            // JSON解析异常
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "请求格式错误: " + e.getMessage());
            try (PrintWriter out = resp.getWriter()) {
                out.print(new Gson().toJson(result));
            }
            e.printStackTrace();
        } catch (Exception e) {
            // 其他异常
            resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "服务器内部错误: " + e.getMessage());
            try (PrintWriter out = resp.getWriter()) {
                out.print(new Gson().toJson(result));
            }
            e.printStackTrace();
        }
    }
}
